package com.hjy.util;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * @author: hujingyu
 * @create: 2024/4/19 14:02
 */
public class SliderListener {

    public static void extracted(Slider slider, TextField textField, String type) {
        if (type.equals("int")) {
            textField.setText(String.valueOf((int) slider.getValue()));
            slider.valueProperty().addListener((ChangeListener<? super Number>) (observableValue, s, t1) -> {
                textField.setText(String.valueOf((int) slider.getValue()));
            });

            //监听文本框值发生变化
            textField.textProperty().addListener((observable, oldValue, newValue) -> {
                if (!newValue.matches("(\\d*)&(-)")) {
                    textField.setText(newValue.replaceAll("(\\d*)&(-)", ""));
                }
            });

            //监听回车建
            textField.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
                if (e.getCode() == KeyCode.ENTER) {
                    String text = textField.getText();
                    if (Integer.parseInt(text) < slider.getMin()) {
                        textField.setText(String.valueOf((int) slider.getMin()));
                        slider.setValue(slider.getMin());
                    } else if (Integer.parseInt(text) > slider.getMax()) {
                        textField.setText(String.valueOf((int) slider.getMax()));
                        slider.setValue(slider.getMax());
                    } else {
                        slider.setValue(Integer.parseInt(text));
                    }
                }
            });

            //监听文本框焦点变化
            textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                    if (!t1) {
                        String text = textField.getText();
                        if (Integer.parseInt(text) < slider.getMin()) {
                            textField.setText(String.valueOf((int) slider.getMin()));
                            slider.setValue(slider.getMin());
                        } else if (Integer.parseInt(text) > slider.getMax()) {
                            textField.setText(String.valueOf((int) slider.getMax()));
                            slider.setValue(slider.getMax());
                        } else {
                            slider.setValue(Integer.parseInt(text));
                        }
                    }
                }
            });
        } else if (type.equals("double")) {

            textField.setText(String.format("%.1f", slider.getValue()));
            slider.valueProperty().addListener((ChangeListener<? super Number>) (observableValue, s, t1) -> {
                textField.setText(String.format("%.1f", slider.getValue()));
            });

            //监听文本框值发生变化
            textField.textProperty().addListener((observable, oldValue, newValue) -> {
                if (!newValue.matches("(\\d*)&(.)") ) {
                    textField.setText(newValue.replaceAll("(\\d*)&(.)", ""));
                }
            });

            //监听回车建
            textField.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
                if (e.getCode() == KeyCode.ENTER) {
                    String text = textField.getText();
                    if (Double.parseDouble(text) < slider.getMin()) {
                        textField.setText(String.valueOf(slider.getMin()));
                        slider.setValue(slider.getMin());
                    } else if (Double.parseDouble(text) > slider.getMax()) {
                        textField.setText(String.valueOf(slider.getMax()));
                        slider.setValue(slider.getMax());
                    } else {
                        slider.setValue(Double.parseDouble(text));
                    }
                }
            });

            //监听文本框焦点变化
            textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                    if (!t1) {
                        String text = textField.getText();
                        if (Double.parseDouble(text) < slider.getMin()) {
                            textField.setText(String.valueOf(slider.getMin()));
                            slider.setValue(slider.getMin());
                        } else if (Double.parseDouble(text) > slider.getMax()) {
                            textField.setText(String.valueOf(slider.getMax()));
                            slider.setValue(slider.getMax());
                        } else {
                            slider.setValue(Double.parseDouble(text));
                        }
                    }
                }
            });
        }

    }


}
